import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JApplet;
import javax.swing.JPanel;

import bluej.BlueJPropStringSource;
import bluej.Config;
import bluej.editor.Editor;
import bluej.editor.EditorWatcher;
import bluej.editor.moe.MoeEditorManager;
import java.util.Properties;
import bluej.parser.entity.ClassLoaderResolver;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class EditorApplet extends JApplet
{
    MoeEditorManager manager;

    EditorWatcher watcher;
    private Object startLock = new Object();

    public class StarterThread extends Thread
    {
        public void run()
        {
            synchronized(startLock)
            {
            Properties cli = new Properties();
            cli.put( "bluej.debug", "true" );
            Config.initialise( new File( "editor/lib" ), cli, false );
            manager = new MoeEditorManager();
            watcher = new EditorWatcher()
            {
                public String getProperty( String prop )
                {
                    return null;
                }


                public void setProperty( String key, String value )
                {
                }


                @Override
                public void modificationEvent( Editor editor )
                {
                    // TODO Auto-generated method stub

                }


                @Override
                public void saveEvent( Editor editor )
                {
                    // TODO Auto-generated method stub

                }


                @Override
                public void closeEvent( Editor editor )
                {
                    // TODO Auto-generated method stub

                }


                @Override
                public String breakpointToggleEvent(
                    Editor editor,
                    int lineNo,
                    boolean set )
                {
                    // TODO Auto-generated method stub
                    return null;
                }


                @Override
                public void compile( Editor editor )
                {
                    // TODO Auto-generated method stub

                }


                @Override
                public void generateDoc()
                {
                    // TODO Auto-generated method stub

                }

            };
            }
        }
    }
    public void start()
    {
        super.start();
        new StarterThread().run();

    }


    public void actionPerformed( ActionEvent e )
    {
        callToAction();

    }


    public void callToAction()
    {
        displayFile( "EditorApplet.java" );

    }

    public class ChangeFileThread extends Thread
    {
        private String fileName;
        public ChangeFileThread(String fileName)
        {
            this.fileName = fileName;
        }
        public void run()
        {
            synchronized(startLock)
            {
            Editor result;
            if ( fileName.endsWith( ".java" ) )
                result = manager.openClass( fileName,
                    fileName,
                    fileName,
                    watcher,
                    false,
                    new Rectangle( 1000, 1000 ),
                    new ClassLoaderResolver( this.getClass()
                        .getClassLoader() ),
                    null );
            else
                result = manager.openText( fileName,
                    fileName,
                    new Rectangle( 1000, 1000 ) );
            result.setVisible( true );
            }
        }
    }
    public void displayFile( String toDisplay )
    {
        new ChangeFileThread(toDisplay).start();
    }
}
